package com.sunday.common.easy.excel.utils;

import jakarta.servlet.http.HttpServletResponse;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * 设置下载文件名称
 *
 * @author zsy
 * @since 2022/12/2
 */
public class DownloadUtils {

    public static void setFileName(HttpServletResponse response, String fileName) throws UnsupportedEncodingException {
        assert response != null : "response must not be null";
        assert fileName != null : "fileName must not be null";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        String encode = URLEncoder.encode(fileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + encode + ".xlsx");
    }

}
